package com.example.project.activity.kaishi;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.example.project.R;
import com.example.project.tool.SharePreferce;
import com.example.project.volley.ConstantUtil;
import com.example.project.volley.Params;
import com.example.project.volley.VolleyInterface;
import com.example.project.volley.VolleyRequest;

import org.json.JSONException;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
//发送验证码页面（修改密码）
public class AlterPassActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText edtEmail,edtYzm; //邮箱文本框，验证码文本框
    private Button butYes,butNo,buteMail; //确定。取消,发送邮件按钮
    //由于Android边编译边生成的原理，将匹配字符串放入全局，作为静态变量可以提高效率
    public static Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    public String yzm;//验证码
    private ProgressDialog pd; //消息提示框

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_pass);
        initView();
    }

    private void initView() {
        edtEmail=findViewById(R.id.edt_xgemail);
        edtYzm=findViewById(R.id.edt_xgyzm);
        butYes=findViewById(R.id.but_yes);
        butYes.setOnClickListener(this);
        butNo=findViewById(R.id.but_no);
        butNo.setOnClickListener(this);
        buteMail=findViewById(R.id.bt_email);
        buteMail.setOnClickListener(this);
        pd = new ProgressDialog(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.but_yes:
                yzm = SharePreferce.getInstance(AlterPassActivity.this).getString("yzm");
                String yzm1=edtYzm.getText().toString().trim();
                if(yzm.equals(yzm1)){
                    Intent intent=new Intent(AlterPassActivity.this, AlterPassUpdateActivity.class);
                    startActivity(intent);
                }else{
                    Toast.makeText(AlterPassActivity.this,"你输入的验证码错误",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.but_no:
                finish();
                break;
            case R.id.bt_email:
                String email=edtEmail.getText().toString().trim();
                if(email.equals("")){
                    Toast.makeText( AlterPassActivity.this,"你输入的不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                if(isEmail(email)){
                    pd = new ProgressDialog(this);
                    pd.setTitle("请稍等");
                    pd.setMessage("正在发送验证码......");
                    pd.show();
                    volleygetEmail(email);
                }else{
                    Toast.makeText(AlterPassActivity.this,"邮箱格式错误",Toast.LENGTH_SHORT).show();
                    break;
                }
                break;
        }
    }

    //验证邮箱优化版
    public static boolean isEmail(String email){
        if (null==email || "".equals(email)) return false;
        Matcher m = p.matcher(email);
        return m.matches();
    }

    //获取邮箱
    private void volleygetEmail(String email) {
        VolleyRequest.RequesetPost( ConstantUtil.GETEMAIL, "GETEMAIL", Params.GetString(email),new VolleyInterface( this ){
            @Override
            public void onMySuccess(String result) throws JSONException {
                pd.dismiss();
                System.out.println("数据:"+result);
                String yzm = result;
                SharePreferce.getInstance(AlterPassActivity.this).setCache("yzm",yzm);
                SharePreferce.getInstance(AlterPassActivity.this).setCache("email",email);
                Toast.makeText(AlterPassActivity.this,"验证码发送成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onMyError(VolleyError error) {
                pd.dismiss();
                Toast.makeText(AlterPassActivity.this, "发送验证码失败", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onImageSuccess(Bitmap bitmap) {
            }
        } );
    }

}